package com.hk.webapp.controller.client;


import com.hk.webapp.annotations.AllowAnonymous;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;

@RestController("ClientCommonController")
public class CommonController extends ClientBaseController {

    @GetMapping("/common/getImg")
    @AllowAnonymous
    public void getImg(String url, HttpServletResponse response) {
        URL httpUrl;
        BufferedInputStream fis;
        try {
            httpUrl = new URL(url);
            fis = new BufferedInputStream(httpUrl.openStream());
            byte[] buffer = new byte[1024];
            int len =0;
            try {
                response.setHeader("Content-Type", "image/jpeg;charset=UTF-8");
                ServletOutputStream outputStream = response.getOutputStream();
                while ((len=fis.read(buffer)) > 0) {
                    outputStream.write(buffer, 0, len);
                }
                outputStream.flush();
            } finally {
                fis.close();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
